#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <FnThread1.h>
#include "PmdGlobals.h"
#include "PhyConstants.h"
#include "wlan_mac.h"
#include "armxdsp.h"
#include "armxdsp-impl.h"
#include "dspmap.h"
#include "rf_drv.h"

extern ST_RF_DRV stRfDrv;
extern float AirRate;
extern float DataRate;
extern hs_int32_t Delta_Payload;
extern hs_int32_t TX_framecnt;
extern hs_int32_t Delta_Tx;
extern hs_int32_t tic1,tic2,tic3,tic4, tic7;
extern hs_uint8_t TxPayload3500[MAX_PAYLOAD_SIZE];
hs_uint8_t TxData[MAX_PAYLOAD_SIZE]={0};
volatile hs_uint32_t TxDataSize = 0;
sem_t sem1;
sem_t sem2;

extern void sendDisplayStaleACK();

void armxdsp_init(void)
{
    if(armxdsp_open_queues() != 0)
    {
    	printf("armxdsp: armxdsp_open_queues failed! \n");
    }
}

int armxdsp_rxdata(uint8_t *data, uint32_t *size)
{
	int i =0;
	unsigned char *p;
    armxdsp_receiver_t r;

    if (armxdsp_receive_begin_dsp(&r, TEST_DSP, ARMXDSP_ALL_SLOTS) != 0)
    {
    	printf("armxdsp: receive data failed!\n");
		return -1;
	}
    else
    {
		p = (unsigned char *)r.data;
		*size = r.size;
		for(i=0; i<r.size; i++)
		{
			data[i] = p[i];
		}
	}

    armxdsp_receive_end_dsp(&r);

    return 0;
}


void armxdsp_txdata(uint8_t *data, uint32_t size)
{
	int status;
	int i = 0;
	uint8_t *p;
	armxdsp_sender_t s;
	status = armxdsp_send_begin_dsp(&s, TEST_DSP, 0, size);
	if (status != 0)
	{
		printf("armxdsp: cannot begin send! \n");
		return;

	}

	p = (uint8_t*)s.data;
	for(i=0; i<size; i++)
	{
		p[i] = data[i];
	}

	armxdsp_send_end_dsp(&s);
}

void rxdata_analyze(uint8_t *buff, uint32_t len)
{
	if(len < 8)
	{
		printf("data_analyze: command analyze error!1 \n");
		return;
	}

	if(buff[0] != 0xaa || buff[1] !=0x55)
	{
		printf("data_analyze: command analyze error!2 \n");
		printf("b0 =%2x, b1 =%2x", buff[0], buff[1]);
		return;
	}

	if((uint8_t)buff[2] == 0x01)
	{
		switch((uint8_t)buff[7])
		{
			case 0x01:
				mac_config_g.datarate = 0x0a;
				break;
			case 0x02:
				mac_config_g.datarate = 0x14;
				break;
			case 0x03:
				mac_config_g.datarate = 0x37;
				break;
			case 0x04:
				mac_config_g.datarate = 0x6e;
				break;
			default:
				mac_config_g.datarate = 0x14;
				break;
		}

		mac_config_g.test_mode = (uint8_t)buff[6];
		if(mac_config_g.test_mode == 1)
		{
			stRfDrv.SetFreq(850000000,800000000);
		}
		printf("test_mode =%d, datarate = %d \n", mac_config_g.test_mode, mac_config_g.datarate);
	}
	else
	{
		if(mac_config_g.test_mode == 1)
		{
			mac_config_g.pkt_size = len+4;
			L1TransmitData(mac_config_g.pkt_size, TxData);
			TX_framecnt++;
			Delta_Tx = tic4-tic3;
			Delta_Payload = tic4-tic7;
			AirRate = ((144+48)+(len*8))*506.88/Delta_Tx;
			DataRate = (len*8)*506.88/Delta_Payload;
		}
	}
}

void *armxdspthread(void *arg)
{
	for(;;)
	{
		uint32_t size = 0;


		if(armxdsp_rxdata(TxData, &size) != 0)
		{
			printf("armxdsp: armxdsp_rxdata failed! \n");
		}
		else
		{
			rxdata_analyze(TxData, size);
			memset(TxData, 0, MAX_PAYLOAD_SIZE);
		}

//		usleep(10000);
		sendDisplayStaleACK();
	}
}

void layer1rxthreadinit(void)
{
	pthread_t armxdsp_thread;
	int ret;
    armxdsp_init();   //armxdsp init!

	ret = pthread_create(&armxdsp_thread, NULL, armxdspthread, NULL);
	if(ret != 0)
	{
		printf("create armxdspthread failed! \n");
		exit(1);
	}
}

